<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="referrer" content="no-referrer" />
    <title>线</title>
    <style>
        body {
            position: absolute;
            margin: 0;
            width: 100%;
            height: 100%;
        }

        #mapContainer {
            left: 0px;
            top: 0px;
            width: 100%;
            height: 100%;
            float: left;
        }
    </style>
</head>

<body>
    <div id="mapContainer" style="position: relative;"></div>
</body>
<script src='/GiswayEngine.js'></script>
<script type="text/javascript">
    window.app = new GE.App('mapContainer', {
        position: [120.170646, 30.28362, 1000], //相机的位置
        lookAt: [120.170646, 30.28362, 0], //相机看向的地图位置
        cameraScrollSpeed: 5, //鼠标滚动时，控制相机移动的速度
        minZoom: 3, //允许地图的最小层级
        maxZoom: 18, //允许地图的最大层级
        ambientLightIntensity: 5,
        useDefaultTexture: true, //是否使用瓦片默认纹理
        background: "Fog", //背景的类型，参数有如下类型：None, Fog, Color, Gradient, Image, Sky, Image。使用Fog地图的性能会比较好
        // backGroundColorA:'#AAAAFF', //当background的值为Gradient时，需要配置该参数。最下端颜色，默认值为#0000FF
        // backGroundColorB:'#0000FF', //当background的值为Gradient时，需要配置该参数。最上端颜色，默认值为#FF0000
        // backGroundColor: '#F5F5F5', //当background的值为Color时，需要配置该参数，默认值为#00BFFF
        // imageUrl: 'http://localhost:9000/examples/assets/img_logo.39362dc1.png', //当background的值为Image时，需要配置该参数。默认值为null
        baseLayer: [
            //影像底图
            {
                url: ' https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', //影像底图的url
                // url: 'http://115.236.87.26:8848/googleTiles/china_image/{z}/{y}/{x}.jpg',
                urlDecimal: true, //影像底图url中的瓦片行列号是否是十进制，true表示十进制，false表示十六进制
                tileType: 'image', //影像底图
                canNotSelect: false, //true表示该图层不参与射线拾取，false表示图层参与射线拾取
            }
        ]
    });

    var polyline;
    var lineWidth = 5;
    function addLine() {
        let pointArray = [ //3857表示web墨卡托坐标系，4326表示经纬度坐标
            new GE.Point(13376713.90129997, 3540579.2959903134, 0, '3857'),
            new GE.Point(13376711.98134067, 3539826.5375406537, 0, '3857'),
            new GE.Point(13377952.49603797, 3539759.3270052206, 0, '3857'),
            new GE.Point(13378008.185227688, 3540675.3111583497, 0, '3857'),
            new GE.Point(13377184.375437068, 3540339.2581293588, 0, '3857'),
            // new GE.Point(13376713.90129997, 3540579.2959903134, 0, '3857'),
        ];
        let lineData = {
            pointArray: pointArray,  //多边形的顶点数组
            color: '#00FA9A',
            lineWidth: lineWidth,  //线的宽度
        };
        polyline = new GE.Polyline(lineData);
        app.add(polyline);
    }

    btn();
    var endPoint = new GE.Point(13377184.375437068, 3540339.2581293588, 0, '3857').toEPSGWeb();
    function btn() {
        let button0 = new GE.Button('为线添加点', function () { //Math.random()
            endPoint.x += Math.random() * 100; //随机修改位置
            endPoint.y += Math.random() * 100;
            
            polyline.addPoint(endPoint);
        });
        app.ui.add(button0);

        let button1 = new GE.Button('删除顶点', function () {
            let pointArray = polyline.getPointArray();
            let removeIndex = prompt('要删除顶点的位置(从0开始)') || -1;
            if(removeIndex == -1 || removeIndex >= pointArray.length){
                alert('请输入合理的顶点位置');
                return;
            }
            polyline.removePoint(removeIndex);
        });
        app.ui.add(button1);

        let button2 = new GE.Button('修改顶点', function () {
            let pointArray = polyline.getPointArray();
            let updateIndex = prompt('要修改顶点的位置(从0开始)') || -1;
            if(updateIndex == -1 || updateIndex >= pointArray.length){
                alert('请输入合理的顶点位置');
                return;
            }
            let updatePoint = polyline.getPointByIndex(updateIndex);
            updatePoint.x += Math.random() * 100;
            updatePoint.y += Math.random() * 100;
            polyline.updatePoint(updateIndex, updatePoint);
        });
        app.ui.add(button2);

        var colorIndex = 0;
        let button3 = new GE.Button('修改颜色', function () {
            let colorArray = ['#FFB6C1', '#DC143C', '#D8BFD8', '#483D8B', '#0000FF'];
            let color = colorArray[colorIndex]; //随机修改位置
            polyline.changeColor(color);

            colorIndex++;
            if(colorIndex >= colorArray.length){
                colorIndex = 0;
            }
        });
        app.ui.add(button3);

        let button4 = new GE.Button('修改宽度', function () {
            let randomValue = randomRange(1, 10); //随机修改线的宽度
            if(randomValue == lineWidth){
                randomValue = randomRange(1, 10); //随机修改线的宽度
            }
            lineWidth = randomValue;
            polyline.changeWidth(lineWidth);
        });
        app.ui.add(button4);
    }

    addLine(); //添加线

    function randomRange(min, max){
        return Math.floor(Math.random() * (max - min)) + min;
    }

</script>

</html>